# -*- coding: utf-8 -*-
"""
@Time    : 2025/1/23 13:57 
@Author  : ZhangShenao 
@File    : tools.py 
@Desc    : 定义工具
"""
from langchain_core.tools import tool


# 使用LangChain提供的@tool装饰器,将一个函数装饰成Tool工具
# @tool装饰器会自动提取函数名作为工具名,函数参数作为工具调用参数
# 并且函数的Docstring会作为工具的描述
@tool
def calculate(expression: str) -> float:
    """Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary"""

    # 使用Python的eval机制计算
    return eval(expression)


@tool
def ask_fruit_unit_price(fruit: str) -> str:
    """Asks the user for the price of a fruit"""
    if fruit.casefold() == "apple":
        return "Apple unit price is 10/kg"
    if fruit.casefold() == "banana":
        return "Banana unit price is 6/kg"
    return f"{fruit} unit price is 20/kg"

# 经过@tool的装饰后,函数就变成了一个Tool工具
# 即拥有name,description,args等属性
# print(calculate.name)
# print(calculate.description)
# print(calculate.args)
